package com.joyoung.sweep.utils.Net;

import com.joyoung.sweep.config.Cons;
import com.joyoung.sweep.model.BaseResult;
import com.joyoung.sweep.model.LoginBean;
import com.joyoung.sweep.model.TokenBean;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class JYHttpDS1 {

    private String json;
    private final String TAG = "JYHttpDS1";


    public interface JoyoungCallBack<T> {
        void Success(T t);

        void failed(String msg);
    }

    /**
     * @param callBack 回调
     */
    public void getToken(final JoyoungCallBack<BaseResult<String>> callBack) {
        String baseUrl = "/rms/business/v3/app/api/auth/token/get";
        String signStr = getSignStr(baseUrl);
        String rmsUrl = noSessionRmsurlThree(signStr);
        TokenBean tokenBean = new TokenBean("5C4A4CFCDF534E1C97FACE4E60B184CC", "axc123!@#");
        if (rmsUrl.equalsIgnoreCase("")) {
            return;
        }
        RetrofitSingleton.getINSTANCE().getRmsJoyoungApi().getToken(rmsUrl, signStr, tokenBean)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(callBack::Success, throwable -> callBack.failed(throwable.getMessage()));
    }

    public void login(String userName, String password,
                      final JoyoungCallBack<BaseResult<String>> callBack) {
        try {
            LoginBean loginBean = new LoginBean(userName, password);

            String baseUrl = "/rms/business/v3/user/api/login/passwd";

            String sign = getSignStr(baseUrl);
            String rmsurlThree = noSessionRmsurlThree(sign);

            RetrofitSingleton.getINSTANCE().getRmsJoyoungApi().login(rmsurlThree, sign, loginBean)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(callBack::Success, throwable -> callBack.failed(throwable.getMessage()));
        } catch (Exception e) {
        }
    }


    private String getSignStr(String baseUrl) {
        long stamp = System.currentTimeMillis();
        baseUrl += "?timestamp=" + stamp;
        return baseUrl;
    }

    private String noSessionRmsurlThree(String baseUrl) {
        return Cons.MY_RMS_URL + baseUrl;
    }
}
